package com.web.test.servlet;

import com.web.test.bean.Student;
import com.web.test.dao.StudentDao;
import com.web.test.utils.SQLUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/9/23.
 */
public class MyInfoServlet extends HttpServlet {

    StudentDao studentDao = new StudentDao();



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Student student = null;

        if (req.getSession().getAttribute("isLogin") != null && (Boolean) req.getSession().getAttribute("isLogin")) {


            String id = (String) req.getSession().getAttribute("id");
            String password = (String) req.getSession().getAttribute("password");

            try {
                student = studentDao.find(SQLUtils.getFindByStuNoAndPassSql(id, password)).get(0);


            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }


            req.getSession().setAttribute("student",student);

//            resp.sendRedirect("webexam_my_info.jsp");
            req.getRequestDispatcher("webexam_my_info.jsp").forward(req, resp);

        } else {
            req.getSession().setAttribute("isLogin", false);
            req.getSession().setAttribute("errorMsg", "请登录后访问页面");
            resp.sendRedirect("webexam_login.jsp");
        }
    }
}
